Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mssql

Package Overview
Dependencies
Maintainers
4
Versions
170
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mssql

Microsoft SQL Server client for Node.js.

  • 8.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
970K
increased by16.34%
Maintainers
4
Weekly downloads
 
Created

What is mssql?

The mssql npm package is a Microsoft SQL Server client for Node.js. It allows you to connect to SQL Server databases, execute queries, and manage transactions. It supports both Promises and async/await syntax, making it versatile for different coding styles.

What are mssql's main functionalities?

Connecting to a SQL Server

This code demonstrates how to connect to a SQL Server database using the mssql package. You need to provide your database credentials and server information in the config object.

const sql = require('mssql');

const config = {
  user: 'your_username',
  password: 'your_password',
  server: 'your_server',
  database: 'your_database'
};

async function connectToDatabase() {
  try {
    let pool = await sql.connect(config);
    console.log('Connected to the database');
  } catch (err) {
    console.error('Database connection failed: ', err);
  }
}

connectToDatabase();

Executing a Query

This code demonstrates how to execute a SQL query using the mssql package. It connects to the database and runs a SELECT query on a specified table.

const sql = require('mssql');

const config = {
  user: 'your_username',
  password: 'your_password',
  server: 'your_server',
  database: 'your_database'
};

async function executeQuery() {
  try {
    let pool = await sql.connect(config);
    let result = await pool.request().query('SELECT * FROM your_table');
    console.log(result);
  } catch (err) {
    console.error('Query execution failed: ', err);
  }
}

executeQuery();

Using Prepared Statements

This code demonstrates how to use prepared statements with the mssql package. Prepared statements are useful for executing queries with parameters, which can help prevent SQL injection attacks.

const sql = require('mssql');

const config = {
  user: 'your_username',
  password: 'your_password',
  server: 'your_server',
  database: 'your_database'
};

async function executePreparedStatement() {
  try {
    let pool = await sql.connect(config);
    let ps = new sql.PreparedStatement(pool);
    ps.input('input_parameter', sql.Int);
    await ps.prepare('SELECT * FROM your_table WHERE id = @input_parameter');
    let result = await ps.execute({ input_parameter: 1 });
    console.log(result);
    await ps.unprepare();
  } catch (err) {
    console.error('Prepared statement execution failed: ', err);
  }
}

executePreparedStatement();

Managing Transactions

This code demonstrates how to manage transactions using the mssql package. Transactions allow you to execute a series of queries as a single unit of work, which can be committed or rolled back based on success or failure.

const sql = require('mssql');

const config = {
  user: 'your_username',
  password: 'your_password',
  server: 'your_server',
  database: 'your_database'
};

async function manageTransaction() {
  try {
    let pool = await sql.connect(config);
    let transaction = new sql.Transaction(pool);
    await transaction.begin();
    let request = new sql.Request(transaction);
    await request.query('INSERT INTO your_table (column1) VALUES (value1)');
    await transaction.commit();
    console.log('Transaction committed');
  } catch (err) {
    console.error('Transaction failed: ', err);
    if (transaction) await transaction.rollback();
  }
}

manageTransaction();

Other packages similar to mssql

Keywords

FAQs

Package last updated on 18 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc